﻿using UnityEngine;

namespace E.Tool
{
    public class AutoLookAtMouse : AutoComponent
    {
        public SpriteRenderer spriteRenderer;

        private void Update()
        {
            if (!IsOwner || !IsSpawned) return;

            Do();
        }

        public override void Do()
        {
            //2d俯视角
            Vector3 dir = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
            float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
            transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

            if (spriteRenderer)
            {
                if (transform.eulerAngles.z >= 00 && transform.eulerAngles.z <= 180)
                {
                    spriteRenderer.flipY = true;
                }
                else
                {
                    spriteRenderer.flipY = false;
                }
            }
        }
    }
}